{% extends "base.html" %}
{# Copyright The IETF Trust 2015-2019, All Rights Reserved #}
{% load origin %}
{% origin %}
{% load ietf_filters static django_bootstrap5 person_filters textfilters %}
{% block title %}Manage open review requests for {{ group.acronym }}{% endblock %}
{% block pagehead %}
    <link rel="stylesheet" href="{% static "ietf/css/list.css" %}">
{% endblock %}
{% block content %}
    {% origin %}
    <h1>
        Manage {{ assignment_status }} open review requests
        <br>
        <small class="text-body-secondary">{{ group.acronym }}</small>
    </h1>
    {% if newly_closed > 0 or newly_opened > 0 or newly_assigned > 0 %}
        <p class="alert alert-danger my-3">
            Changes since last refresh:
            {% if newly_closed %}{{ newly_closed }} request{{ newly_closed|pluralize }} closed.{% endif %}
            {% if newly_opened %}{{ newly_opened }} request{{ newly_opened|pluralize }} opened.{% endif %}
            {% if newly_assigned %}{{ newly_assigned }} request{{ newly_assigned|pluralize }} changed assignment.{% endif %}
            {% if saving %}Check that you are happy with the results, then re-save.{% endif %}
        </p>
    {% endif %}
    {% if review_requests %}
        <form class="review-requests" method="post">
            {% csrf_token %}
            {% for r in review_requests %}
                <div class="my-3 card review-request">
                    <div class="card-header">
                        <span class="float-end">
                            {% if r.in_lc_and_telechat %}
                                Last Call and telechat
                            {% else %}
                                {{ r.type.name }}
                            {% endif %}
                            deadline {{ r.deadline|date:"Y-m-d" }}
                            {% if r.due %}<span class="badge rounded-pill text-bg-warning">{{ r.due }} day{{ r.due|pluralize }}</span>{% endif %}
                        </span>
                        <a href="{% if r.requested_rev %}{% url "ietf.doc.views_doc.document_main" name=r.doc.name rev=r.requested_rev %}{% else %}{% url "ietf.doc.views_doc.document_main" name=r.doc.name %}{% endif %}?include_text=1">
                            {{ r.doc.name }}-
                            {% if r.requested_rev %}
                                {{ r.requested_rev }}
                            {% else %}
                                {{ r.doc.rev }}
                            {% endif %}
                        </a>
                    </div>
                    <div class="card-body">
                        <div class="row request-metadata">
                            <div class="col-sm-6">
                                <p class="lead">
                                    {{ r.doc.title|urlize_ietf_docs|linkify }}
                                </p>
                                {% if r.pk != None %}
                                    <span class="fw-bold">Requested:</span>
                                    <a href="{% url "ietf.doc.views_review.review_request" name=r.doc.name request_id=r.pk %}">
                                        {{ r.time|date:"Y-m-d" }}
                                    </a>
                                    {% if r.requested_by %}
                                        by {% person_link r.requested_by %}
                                    {% endif %}
                                    <br>
                                {% else %}
                                    <span class="badge rounded-pill text-bg-info">Auto-suggested</span>
                                    <br>
                                {% endif %}
                                {% if r.doc.authors %}
                                    <span class="fw-bold">Authors:</span>
                                    {% for person in r.doc.authors %}
                                        {% person_link person %}{% if not forloop.last %},{% endif %}
                                    {% endfor %}
                                    <br>
                                {% endif %}
                                {% if r.doc.group.type_id != "individ" %}
                                    <span class="fw-bold">{{ r.doc.group.type.name }}:</span>
                                    <a href="{% url "ietf.group.views.group_home" acronym=r.doc.group.acronym %}">
                                        {{ r.doc.group.acronym }}
                                    </a>
                                    <br>
                                {% endif %}
                                {% if r.wg_chairs %}
                                    <span class="fw-bold">{{ r.doc.group.type.name }} chairs:</span>
                                    {% for person in r.wg_chairs %}
                                        {% person_link person %}{% if not forloop.last %},{% endif %}
                                    {% endfor %}
                                    <br>
                                {% endif %}
                                {% if r.latest_reqs %}
                                    {% for rlatest in r.latest_reqs %}
                                        {% for alatest in rlatest.reviewassignment_set.all %}
                                            <div>
                                                {% if alatest.reviewed_rev %}
                                                    Previous review of
                                                    <a href="{% url "ietf.doc.views_doc.document_main" name=rlatest.doc.name rev=alatest.reviewed_rev %}?include_text=1">
                                                        {% if rlatest.doc.name != r.doc.name %}{{ rlatest.doc.name }}{% endif %}
                                                        -{{ alatest.reviewed_rev }}
                                                    </a>
                                                    {% if alatest.reviewed_rev != r.doc.rev %}
                                                        (<a href="{{ rfcdiff_base_url }}?url1={{ rlatest.doc.name }}-{{ alatest.reviewed_rev }}&amp;url2={{ r.doc.name }}-{{ r.doc.rev }}">diff</a>)
                                                    {% endif %}
                                                    :
                                                    <a href="{{ alatest.review.get_absolute_url }}">
                                                        {% if alatest.result %}
                                                            {{ alatest.result.name }}
                                                        {% else %}
                                                            result unavail.
                                                        {% endif %}
                                                    </a>
                                                    by {% person_link alatest.reviewer.person %}
                                                    {% if alatest.closed_review_request_event %}{{ alatest.closed_review_request_event.time.date|date }}{% endif %}
                                                {% else %}
                                                    Previous review of
                                                    <a href="{% url "ietf.doc.views_doc.document_main" name=rlatest.doc.name %}?include_text=1">
                                                        {% if rlatest.doc.name != r.doc.name %}
                                                            {{ rlatest.doc.name }}
                                                        {% else %}
                                                            this document
                                                        {% endif %}
                                                    </a>
                                                    :
                                                    <a href="{% url "ietf.doc.views_review.review_request" name=rlatest.doc.name request_id=rlatest.pk %}">
                                                        {% if alatest.result %}
                                                            {{ alatest.result.name }}
                                                        {% else %}
                                                            result unavail.
                                                        {% endif %}
                                                    </a>
                                                    by {% person_link alatest.reviewer.person %}
                                                    {% if alatest.closed_review_request_event %}{{ alatest.closed_review_request_event.time.date|date }}{% endif %}
                                                {% endif %}
                                            </div>
                                        {% endfor %}
                                    {% endfor %}
                                {% endif %}
                                <div>
                                    <span class="badge rounded-pill text-bg-secondary">{{ r.doc.pages }} page{{ r.doc.pages|pluralize }}</span>
                                    <span class="badge rounded-pill text-bg-info">{{ r.doc.friendly_state }}</span>
                                </div>
                                {% if r.doc.telechat_date %}<span class="badge rounded-pill text-bg-warning">IESG telechat {{ r.doc.telechat_date }}</span>{% endif %}
                                {% if r.comment %}<pre class="border p-3 pasted">{{ r.comment }}</pre>{% endif %}
                            </div>
                            <div class="col-sm-6">
                                <span class="fw-bold">Abstract:</span>
                                {{ r.doc.abstract|urlize_ietf_docs|linkify }}
                            </div>
                        </div>
                        {% if r.form.non_field_errors %}
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="alert alert-danger my-3">
                                        {% for e in r.form.non_field_errors %}{{ e }}{% endfor %}
                                    </div>
                                </div>
                            </div>
                        {% endif %}
                    </div>
                    <input type="hidden" name="reviewrequest" value="{{ r.pk }}">
                    <input type="hidden"
                           name="{{ r.form.prefix }}-existing_reviewer"
                           value="{{ r.reviewer_id|default:"" }}">
                    <div class="card-footer">
                        <div class="d-flex">
                            <div class="assign-action mx-1">
                                {% if r.reviewer %}
                                    <button type="button"
                                            class="btn btn-sm btn-primary btn-success"
                                            title="Click to reassign reviewer">
                                        {% person_link r.reviewer.person %}
                                    </button>
                                    {% if r.state_id == "accepted" %}<span class="badge rounded-pill text-bg-secondary">Accepted</span>{% endif %}
                                    {% if r.reviewer_unavailable %}<span class="badge rounded-pill text-bg-danger">Unavailable</span>{% endif %}
                                {% else %}
                                    <button type="button"
                                            class="btn btn-sm btn-primary btn-success"
                                            title="Click to assign reviewer">
                                        Assign reviewer
                                    </button>
                                {% endif %}
                                {% bootstrap_field r.form.action layout="horizontal" size="sm" %}
                            </div>
                            <div class="reviewer-controls mx-1 flex-fill row">
                                {% if r.form.review_type %}
                                    {% bootstrap_field r.form.review_type layout="horizontal" wrapper_class="col" size="sm" %}
                                {% endif %}
                                {% bootstrap_field r.form.reviewer layout="horizontal" wrapper_class="col row" label_class="p-0" size="sm" %}
                                {% bootstrap_field r.form.add_skip layout="horizontal" wrapper_class="col col-2" size="sm" %}
                                <div class="col col-1">
                                    <button type="button"
                                            class="btn btn-sm btn-primary undo"
                                            title="Cancel assignment"
                                            data-initial="{{ r.form.fields.reviewer.initial|default:"" }}">
                                        Cancel
                                    </button>
                                </div>
                                {% if r.form.reviewer.errors or r.form.add_skip.errors %}
                                    <div class="alert alert-danger my-3">
                                        {% for e in r.form.reviewer.errors %}{{ e }}{% endfor %}
                                        {% for e in r.form.add_skip.errors %}{{ e }}{% endfor %}
                                    </div>
                                {% endif %}
                            </div>
                            <div class="close-action mx-1">
                                <button type="button" class="btn btn-sm btn-primary btn-danger">Close</button>
                                {% if r.doc.past_telechat_date %}(was on {{ r.doc.past_telechat_date }} telechat){% endif %}
                            </div>
                            <div class="close-controls mx-1 flex-fill">
                                {% bootstrap_field r.form.close layout="horizontal" size="sm" %}
                                {% bootstrap_field r.form.close_comment layout="horizontal" size="sm" %}
                                <button type="button"
                                        class="btn btn-sm btn-primary undo"
                                        title="Cancel closing">Cancel</button>
                                {% if r.form.close.errors %}
                                    {% bootstrap_field r.form.close.errors layout="horizontal" size="sm" %}
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            {% endfor %}
            <a href="{% url "ietf.group.views.review_requests" group_type=group.type_id acronym=group.acronym %}"
               class="btn btn-secondary float-end">
                Back
            </a>
            <button class="btn btn-primary" type="submit" name="action" value="save">Save changes</button>
            <button class="btn btn-primary"
                    type="submit"
                    name="action"
                    value="save-continue">Save and continue editing</button>
            <button class="btn btn-primary" type="submit" name="action" value="refresh">Refresh (keeping changes)</button>
        </form>
    {% else %}
        <p>
            There are currently no {{ assignment_status }} open requests.
        </p>
    {% endif %}
{% endblock %}
{% block js %}
    <script src="{% static "ietf/js/list.js" %}"></script>
    <script src="{% static "ietf/js/manage-review-requests.js" %}">
    </script>
{% endblock %}